package com.he.heblog.controller;

import com.he.heblog.dto.UserDto;
import com.he.heblog.entity.*;
import com.he.heblog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;

@RequestMapping("/comment")
@Controller
public class CommentController {

    @Autowired
    CommentService commentService;
    @Autowired
    ChildlistService childlistService;
    @Autowired
    ChildcommentService childcommentService;
    @Autowired
    NotificationService notificationService;
    @Autowired
    ArticleService articleService;
//    新增评论
    @PostMapping("/add")
    @ResponseBody
    public String addComment(Comment comment){
        //    获取登录用户id
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
         //    转成用户类型
        UserDto principal = (UserDto) authentication.getPrincipal();
        comment.setUserID(principal.getId());
        commentService.save(comment);
//        通知文章的发布者有人新增了评论
//        通过文章ID查询文章拥有者
        Article byId = articleService.getById(comment.getArticleID());
        Integer userid = byId.getUserid();
//        新增通知
        Notification notification = new Notification();
        notification.setFromId(principal.getId());
        notification.setToId(userid);
        notification.setSubject(1);
        notification.setTime(LocalDateTime.now());
        notificationService.save(notification);
        return "success";
    }
//    跳转到回复页面
//    需要文章ID，评论ID,评论所属用户名
    @GetMapping("/reply/{articleID}/{commentID}/{username}")
   public String toReply(@PathVariable Integer articleID, @PathVariable Integer commentID, @PathVariable String username, Model model){

        model.addAttribute("articleID",articleID);
        model.addAttribute("commentID",commentID);
        model.addAttribute("username",username);
        return "front/user/reply";
    }
//提交回复子评论
//评论对应的文章，评论对应的主评论，发表评论的用户，评论的内容
    @ResponseBody
    @PostMapping("/replyComment")
    public String replyComment(Integer commentID, Childlist childlist)
    {
//        获取当前登录用户
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserDto principal = (UserDto) authentication.getPrincipal();
        Integer id1 = principal.getId();
        childlist.setUserID(id1);
//        新增到子评论表
        boolean save = childlistService.save(childlist);
        if(save)
        {
            Integer id = childlist.getId();
//            拿到子评论ID，插入评论子评论中间表
            Childcomment childcomment = new Childcomment();
            childcomment.setCommentID(commentID);
            childcomment.setChildID(id);
            boolean save1 = childcommentService.save(childcomment);
//            新增成功后返回到当前文章详情页
            return "success";
        }
//
        else
            return "false";



    }







}
